package __正则

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"io/ioutil"
	"net/http"
	"os"
	"regexp"
	"strings"
	"time"
	"xorm.io/xorm"
)

// 获取数据
func GetData(url string) string {
	client := &http.Client{} //定义客户端
	req, _ := http.NewRequest("GET", url, nil)

	//设置header和cookie
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
	req.Header.Add("Cookie", "")

	//发出请求
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Http get err", err)
		return ""
	}

	//判断返回的状态码
	if resp.StatusCode != 200 {
		fmt.Println("Http status code", resp.StatusCode)
		return ""
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Read error", err)
		return ""
	}
	return string(body)
}

// 解析数据
func Parse(html string) {
	//替换掉空格
	html = strings.Replace(html, "\n", "", -1)

	//边栏内容块正则
	re_sidebar := regexp.MustCompile(`<aside id="sidebar" role="navigation">(.*?)</aside>`)

	//找到边栏内容块
	sidebar := re_sidebar.FindString(html)

	//连接正则
	re_link := regexp.MustCompile(`href="(.*?)"`)

	//找到所有连接
	links := re_link.FindAllString(sidebar, -1)
	base_url := "https://gorm.io/zh_CN/docs/"
	for _, v := range links {
		fmt.Printf("v:%v\n", v)
		s := v[6 : len(v)-1]
		url := base_url + s
		fmt.Printf("url:%v\n", url)

		body := GetData(url)

		//启动另外一个线程处理
		go Parse2(body)
	}

}

func Parse2(html string) {
	//替换掉空格
	html = strings.Replace(html, "\n", "", -1)

	//页面内容
	re_content := regexp.MustCompile(`<div class="article">(.*?)<div>`)

	//找到页面内容
	content := re_content.FindString(html)

	//提取标题
	re_title := regexp.MustCompile(`<h1 class="article-title" itemprop="name">(.*?)</h1>`)

	//找到页面内容
	title := re_title.FindString(content)
	fmt.Println(title)

	//切片
	title = title[42 : len(title)-5]
	fmt.Printf("标题切片:%v\n", title)

	//(1)保存到本地
	Save(title, content)

	//(2)保存到数据库
	SaveToDB(title, content)
}

// 保存到本地
func Save(body string, content string) {
	err := os.WriteFile("./pages/"+body+".html", []byte(content), 0644)
	if err != nil {
		panic(err)
	}
}

// 保存到数据库
var engine *xorm.Engine
var err error

type GormPage struct {
	Id      int64
	Title   string
	Connect string    `xorm:"text"`
	Created time.Time `xorm:"created"`
	Updated time.Time `xorm:"updated"`
}

func init() {
	engine, err := xorm.NewEngine("mysql", "root:123456@/go_db?charset=utf8mb4")
	if err != nil {
		fmt.Printf("err:%v\n", err)
	} else {
		err2 := engine.Ping()
		if err2 != nil {
			fmt.Printf("err2:%v\n", err2)
		} else {
			print("连接成功...")
		}
	}
}

func SaveToDB(title string, content string) {
	engine.Sync(new(GormPage))
	page := GormPage{
		Title:   title,
		Connect: content,
	}
	n, err := engine.Insert(&page)
	if err != nil {
		fmt.Println("插入数据库失败：", err)
	}
	fmt.Printf("成功保存了:%v条数据", n)
}

func main() {
	url := "https://gorm.io/zh_CN/docs/"
	data := GetData(url)
	Parse(data)
}
